package com.d3works.apps.account.web;

import com.d3works.apps.account.db.AnalyticAccount;
import com.d3works.apps.account.db.AnalyticAxis;
import com.d3works.apps.account.db.repo.AnalyticAccountRepository;
import com.d3works.apps.account.db.repo.AnalyticAxisRepository;
import com.d3works.apps.account.exception.AccountExceptionMessage;
import com.d3works.apps.account.service.AnalyticAxisControlService;
import com.d3works.apps.account.service.analytic.AnalyticAccountService;
import com.d3works.apps.account.service.analytic.AnalyticAxisService;
import com.d3works.exception.D3worksException;
import com.d3works.exception.ResponseMessageType;
import com.d3works.exception.service.TraceBackService;
import com.d3works.i18n.I18n;
import com.d3works.inject.Beans;
import com.d3works.rpc.ActionRequest;
import com.d3works.rpc.ActionResponse;
import java.util.List;

public class AnalyticAxisController {

  public void checkCompanyOnMoveLine(ActionRequest request, ActionResponse response)
      throws D3worksException {
    try {

      AnalyticAxis analyticAxis = request.getContext().asType(AnalyticAxis.class);

      if (analyticAxis != null && analyticAxis.getCompany() != null) {
        if (Beans.get(AnalyticAxisService.class).checkCompanyOnMoveLine(analyticAxis)) {
          response.setError(
              I18n.get(
                  "This axis already contains Analytic Move Lines attached to several companies. Please make sure to correctly reassign the analytic move lines currently attached to this axis to another axis before being able to assign other."));
          response.setValue("company", null);
        }
      }
    } catch (Exception e) {
      TraceBackService.trace(response, e);
    }
  }

  public void controlUnicity(ActionRequest request, ActionResponse response) {
    try {
      AnalyticAxis analyticAxis = request.getContext().asType(AnalyticAxis.class);
      Beans.get(AnalyticAxisControlService.class).controlUnicity(analyticAxis);

    } catch (Exception e) {
      TraceBackService.trace(response, e, ResponseMessageType.ERROR);
    }
  }

  public void setReadOnly(ActionRequest request, ActionResponse response) {
    try {
      AnalyticAxis analyticAxis =
          Beans.get(AnalyticAxisRepository.class)
              .find(request.getContext().asType(AnalyticAxis.class).getId());
      if (analyticAxis != null) {
        Boolean isInMove =
            Beans.get(AnalyticAxisControlService.class).isInAnalyticMoveLine(analyticAxis);
        response.setAttr("name", "readonly", isInMove);
        response.setAttr("code", "readonly", isInMove);
      }

    } catch (Exception e) {
      TraceBackService.trace(response, e, ResponseMessageType.ERROR);
    }
  }

  public void removeSameAnalyticGrouping(ActionRequest request, ActionResponse response) {
    try {
      AnalyticAxis analyticAxis = request.getContext().asType(AnalyticAxis.class);
      String analyticGroupingChanged = request.getContext().get("_source").toString();
      List<Integer> analyticGroupingToRemoveList =
          Beans.get(AnalyticAxisService.class)
              .getSameAnalyticGroupingValues(analyticAxis, analyticGroupingChanged);
      for (Integer value : analyticGroupingToRemoveList) {
        response.setValue("analyticGrouping" + value, null);
      }
    } catch (Exception e) {
      TraceBackService.trace(response, e);
    }
  }

  public void checkAnalyticAccountCompany(ActionRequest request, ActionResponse response) {
    try {
      AnalyticAxis analyticAxis = request.getContext().asType(AnalyticAxis.class);
      if (analyticAxis.getId() != null
          && analyticAxis.getCompany() != null
          && !analyticAxis
              .getCompany()
              .equals(
                  Beans.get(AnalyticAxisRepository.class)
                      .find(analyticAxis.getId())
                      .getCompany())) {
        List<AnalyticAccount> childrenList =
            Beans.get(AnalyticAccountRepository.class).findByAnalyticAxis(analyticAxis).fetch();

        if (Beans.get(AnalyticAccountService.class)
            .checkChildrenAccount(analyticAxis.getCompany(), childrenList)) {
          response.setError(
              I18n.get(AccountExceptionMessage.ANALYTIC_AXIS_ACCOUNT_ERROR_ON_COMPANY));
        }
      }
    } catch (Exception e) {
      TraceBackService.trace(response, e);
    }
  }
}
